import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)  # 连接数据库
db = client.info    # 指定数据库名称

# result1 = db.students.insert_one({'name': 'Jordan', 'age': 23, 'sex': '男'})    # mongo指令
# result2 = db.students.insert_one({'name': 'Mike', 'age': 22, 'sex': '男'})
# result3 = db.students.insert_one({'name': 'Mary', 'age': 21, 'sex': '女'})
# result = db.students.find_one({'name': 'Jordan'}) # 查找一条数据
# result = db.students.find({'age': {'$gt': 22}})   # 条件搜索（gt > greater than > 大于）
# result = db.students.find({'name': 'Jordan'})   # find方法返回cursor，需要遍历打印出其值
# for r in result:
#     print(r)
# print(result)

count = db.students.count_documents({'age': 23})  # 计数方法
print(count)

# sort_result = db.studens.find().sort('name', pymongo.ASCENDING)    # 升序排序
# for r in sort_result:
#     print(r)

# delete = db.students.delete_one({})   # 删除一条数据

